package com.dingwei.superiordiary.mvp.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.base.MyBaseBean;
import com.dingwei.superiordiary.bean.FailsBean;
import com.dingwei.superiordiary.bean.MoreReplyBean;
import com.dingwei.superiordiary.custom.myCallback;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.dingwei.superiordiary.ui.activity.LoginActivity;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.base.URLS;
import com.lihao.baselibrary.base.basePresenter;
import com.lihao.baselibrary.utils.L;
import com.lihao.baselibrary.utils.ScreenUtils;

import org.xutils.http.RequestParams;
import org.xutils.x;

import static com.scwang.smartrefresh.layout.util.DensityUtil.dp2px;

/**
 * Created by lihao on 2017/9/12.
 */

public class MoreReplyPresenter extends basePresenter {

    private Contract.CommentMoreReplyView moreReplyView;
    private Dialog bottomDialog;

    public MoreReplyPresenter(Contract.CommentMoreReplyView view) {
        super(view);
        this.moreReplyView = view;
    }

    public void showEvaluationDialog(final String id, final String pid) {

        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_evaluation, null);
        final EditText etEvaluation = (EditText) view.findViewById(R.id.et_evaluation);
        Button btnAddComment = (Button) view.findViewById(R.id.btn_add_comment);
        btnAddComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                saveComment(id, pid, etEvaluation.getText().toString());
                bottomDialog.dismiss();
            }
        });
        view.measure(0, 0);
        bottomDialog = new Dialog(BaseActivity.context, R.style.Theme_Light_Dialog);
        bottomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        bottomDialog.setContentView(view);
        Window dialogWindow = bottomDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        bottomDialog.show();
    }


    public void saveComment(String id, final String pid, final String content) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.SAVE_COMMENT);
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("pid", pid);
        rp.addBodyParameter("content", content);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                moreReplyView.replySuccess(content);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {

            }
        });
    }

    public void initData(String pid, int page,int type, final boolean isRefresh, boolean isShowDialog) {

        RequestParams rp = new RequestParams(URLS.HOST + URLS.SUB_COMMENT_LIST);
        rp.addBodyParameter("id", pid);
        rp.addBodyParameter("type", type+"");
        rp.addBodyParameter("page", page + "");
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context, MoreReplyBean.class, isShowDialog, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                MoreReplyBean bean = (MoreReplyBean) baseBean;
                if (isRefresh) {
                    moreReplyView.OnRefresh(isRefresh, bean);
                } else {
                    moreReplyView.OnLoadMore(isRefresh, bean);
                }

            }

            @Override
            public void fail(FailsBean baseBean, String result) {
                moreReplyView.onFail();
            }
        });
    }
    public void commentLike(String id,final int position) {
        if (!BaseActivity.value.getIsLogin()) {
            Intent intent = new Intent(BaseActivity.context, LoginActivity.class);
            BaseActivity.context.startActivity(intent);
            return;
        }
        RequestParams rp = new RequestParams(URLS.HOST + URLS.LIKES_COMMENT);
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                moreReplyView.commentLikeSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void showReportDialog(Activity activity, int own) {
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_choose_photo, null);

        TextView tv_photograph = (TextView) view.findViewById(R.id.tv_photograph);
        TextView tv_album = (TextView) view.findViewById(R.id.tv_album);
        TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_photograph.setText("举报");
        tv_album.setText("屏蔽此评论");
        if (own == 2){
            tv_album.setVisibility(View.VISIBLE);
        }else {
            tv_album.setVisibility(View.GONE);
        }
        L.e("--------->"+own);
        tv_photograph.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                //举报
                moreReplyView.Report();
            }
        });
        tv_album.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
                //屏蔽此评论
                moreReplyView.shield();
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bottomDialog.dismiss();
            }
        });

        bottomDialog = new Dialog(activity, R.style.Theme_Light_Dialog);
        bottomDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        bottomDialog.setContentView(view);
        Window dialogWindow = bottomDialog.getWindow();
        //设置dialog在屏幕底部
        dialogWindow.setGravity(Gravity.BOTTOM);
        //设置dialog弹出时的动画效果，从屏幕底部向上弹出
        dialogWindow.setWindowAnimations(R.style.dialogstyle);
        dialogWindow.getDecorView().setPadding(0, 0, 0, 0);
        //获得window窗口的属性
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        //设置窗口宽度为充满全屏
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        //设置窗口高度为包裹内容
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        //将设置好的属性set回去
        dialogWindow.setAttributes(lp);
        bottomDialog.show();
    }

    public void shieldComment(String id) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.SHIELD_COMMENT);
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                moreReplyView.shieldCommentSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }
    private PopupWindow popupWindow;
    public void showSortPopupWindow(View tvShow) {
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.dialog_posts_details_sort, null);
        LinearLayout llbg = (LinearLayout) view.findViewById(R.id.ll_bg);
        TextView hot = (TextView) view.findViewById(R.id.hot);
        TextView reversesequence = (TextView) view.findViewById(R.id.reverse_sequence);
        TextView positivesequence = (TextView) view.findViewById(R.id.positive_sequence);
        hot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                moreReplyView.SetHotSort();
            }
        });
        reversesequence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                moreReplyView.SetDaoSort();
            }
        });
        positivesequence.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
                moreReplyView.SetZhengSort();
            }
        });
        popupWindow = new PopupWindow(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        int windowPos[] = calculatePopWindowPos(tvShow, view, llbg);
        popupWindow.showAtLocation(view, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
        backgroundAlpha(0.4f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });
    }

    private void backgroundAlpha(float f) {
        WindowManager.LayoutParams lp = BaseActivity.context.getWindow().getAttributes();
        lp.alpha = f;
        BaseActivity.context.getWindow().setAttributes(lp);
    }

    /**
     * 计算出来的位置，y方向就在anchorView的上面和下面对齐显示，x方向就是与屏幕右边对齐显示
     * 如果anchorView的位置有变化，就可以适当自己额外加入偏移来修正
     *
     * @param anchorView  呼出window的view
     * @param contentView window的内容布局
     * @return window显示的左上角的xOff, yOff坐标
     */
    private int[] calculatePopWindowPos(final View anchorView, final View contentView, LinearLayout llBg) {
        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 获取锚点View在屏幕上的左上角坐标位置
        anchorView.getLocationOnScreen(anchorLoc);
        final int anchorHeight = anchorView.getHeight();
        // 获取屏幕的高宽
        final int screenHeight = ScreenUtils.getScreenHeight(anchorView.getContext());
        final int screenWidth = ScreenUtils.getScreenWidth(anchorView.getContext());
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 计算contentView的高宽
        final int windowHeight = contentView.getMeasuredHeight();
        final int windowWidth = contentView.getMeasuredWidth();
        // 判断需要向上弹出还是向下弹出显示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] - windowHeight;
            llBg.setPadding(0, 0, 0, dp2px(10));
            llBg.setBackgroundResource(R.drawable.pic2t);
        } else {
            llBg.setPadding(0, dp2px(10), 0, 0);
            llBg.setBackgroundResource(R.drawable.pic1j);
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        return windowPos;
    }




    public void showMeunPopupWindow(View tvShow,int is_collect) {
        View view = LayoutInflater.from(BaseActivity.context).inflate(R.layout.pop_meun, null);
        LinearLayout llbg = (LinearLayout) view.findViewById(R.id.ll_bg);
        ImageView imgreport = (ImageView) view.findViewById(R.id.img_report);
        ImageView imgshare = (ImageView) view.findViewById(R.id.img_share);
        final ImageView imgcollect = (ImageView) view.findViewById(R.id.img_collect);
        if (is_collect==1){
            imgcollect.setImageResource(R.mipmap.ic_details_collect);
        }else {
            imgcollect.setImageResource(R.mipmap.ic_details_collectno);
        }
        imgcollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                moreReplyView.collect();
            }
        });
        imgshare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                moreReplyView.share();
            }
        });
        imgreport.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                popupWindow.dismiss();
                moreReplyView.report();
            }
        });

        popupWindow = new PopupWindow(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
        popupWindow.setContentView(view);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);

        final int windowPos[] = new int[2];
        final int anchorLoc[] = new int[2];
        // 获取锚点View在屏幕上的左上角坐标位置
        tvShow.getLocationOnScreen(anchorLoc);
        final int anchorHeight = tvShow.getHeight();
        // 获取屏幕的高宽
        final int screenHeight = ScreenUtils.getScreenHeight(tvShow.getContext());
        final int screenWidth = ScreenUtils.getScreenWidth(tvShow.getContext());
        view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        // 计算contentView的高宽
        final int windowHeight = view.getMeasuredHeight();
        final int windowWidth = view.getMeasuredWidth();
        // 判断需要向上弹出还是向下弹出显示
        final boolean isNeedShowUp = (screenHeight - anchorLoc[1] - anchorHeight < windowHeight);
        if (isNeedShowUp) {
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] - windowHeight;
            llbg.setPadding(0, 0, 0, dp2px(10));
            llbg.setBackgroundResource(R.drawable.pic2t);
        } else {
            llbg.setPadding(0, dp2px(10), 0, 0);
            llbg.setBackgroundResource(R.mipmap.ic_detials_pop_bg);
            windowPos[0] = screenWidth - windowWidth;
            windowPos[1] = anchorLoc[1] + anchorHeight;
        }
        popupWindow.showAtLocation(view, Gravity.TOP | Gravity.START, windowPos[0], windowPos[1]);
        backgroundAlpha(0.4f);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1);
            }
        });
    }


    public void activityCollect(String id,int is_collect) {
        RequestParams rp = new RequestParams(URLS.HOST + URLS.ActivityCollect);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());
        rp.addBodyParameter("id", id);
        if (is_collect==1){
            rp.addBodyParameter("action", "0");
        }else {
            rp.addBodyParameter("action", "1");
        }
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                showToast(baseBean.message);
                moreReplyView.collectSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }


    public void commentCai(String id, final int position) {
        if (!BaseActivity.value.getIsLogin()) {
            Intent intent = new Intent(BaseActivity.context, LoginActivity.class);
            BaseActivity.context.startActivity(intent);
            return;
        }
        RequestParams rp = new RequestParams(URLS.HOST + URLS.trampleComment);
        rp.addBodyParameter("id", id);
        rp.addBodyParameter("sessionid", BaseActivity.value.getToken());

        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                moreReplyView.trampleCommentSuccess(position);
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

    public void attentionAction(String consumer_id, String action) {
        RequestParams rp = new RequestParams(URLS.HOST+URLS.attentionAction);
        rp.addBodyParameter("userid", consumer_id);
        rp.addBodyParameter("action", action);
        rp.addBodyParameter("sessionid",BaseActivity.value.getToken());
        x.http().post(rp, new myCallback(BaseActivity.context, MyBaseBean.class, rp) {
            @Override
            public void back(MyBaseBean baseBean, String result) {
                moreReplyView.attentionActionSuccess();
            }

            @Override
            public void fail(FailsBean baseBean, String result) {
            }
        });
    }

}
